﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using TextBox = System.Windows.Forms.TextBox;

namespace Windows程序
{
    public partial class Form1 : Form
    {

        int num = 20; // 题数
        int answer = 0; // 每道题的答案
        int time = 5; // 每道题的时间
        int point = 0; // 得分

        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e) //初始化
        {
        }

        private void button1_Click(object sender, EventArgs e) //开始按钮
        {
            label2.Visible = true;
            label3.Visible = true;
            label4.Visible = true;
            label5.Visible = true;
            label6.Visible = true;
            label7.Visible = true;
            label8.Visible = true;
            label9.Visible = true;
            label10.Visible = true;
            button2.Visible = true;
            button3.Visible = true;
            textBox1.Visible = true;

            point = 0;
            timer1.Start();
            this.button2_Click(sender, e);
        }

        private void button3_Click(object sender, EventArgs e) // 结束按钮
        {
            num = 20;
            answer = 0;

            button1.Visible = true;

            button2.Visible = false;
            button3.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
            label6.Visible = false;
            label7.Visible = false;
            label8.Visible = false;
            label9.Visible = false;
            label10.Visible = false;
            textBox1.Visible = false;

            timer1.Stop();
        }

        private async void button2_Click(object sender, EventArgs e)
        {

            if (num < 20) //防止第一次进入判断结果
            {
                if (textBox1.Text.Equals("") || int.Parse(textBox1.Text) != answer)
                {
                    textBox1.BackColor = Color.Red;
                    textBox1.Text = answer.ToString();

                    button2.Enabled = false;
                    await Task.Delay(1000);

                }
                else if (int.Parse(textBox1.Text) == answer)
                {
                    //加分
                    point = point + 5;
                    label7.Text ="得分" + point.ToString()+"/100";

                    textBox1.BackColor = Color.Green;

                    button2.Enabled=false;
                    await Task.Delay(500);
                }
            }

            if (num == 0)
            {
                button3_Click(sender, e);
            }
            else
            {
                label2.Text = "剩余题数：" + num.ToString();

                Random number = new Random();

                //生成随机数 和 符号
                label3.Text =  number.Next(0, 100).ToString();
                label4.Text = number.Next(0, 100).ToString();
                label5.Text = number.Next(0, 100) > 50 ? "+" : "-";

                //刷新时间
                time = 5;

                if (label5.Text == "+")
                {
                    answer = int.Parse(label3.Text) + int.Parse(label4.Text);
                }
                else
                {
                    answer = int.Parse(label3.Text) - int.Parse(label4.Text);
                }

                button2.Enabled = true;
                textBox1.BackColor = Color.White;
                num--;
                textBox1.Text = "";
            }
        }


        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {   


            if((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == 8 || e.KeyChar=='-')
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (time >= 0)
            {
                label9.Text = time.ToString();
                time--;
            }
            else
            {
                button1_Click(sender, e);
                time = 5;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
               
            if (textBox != null)
            {
                    string text = textBox.Text;
                    text = text.Replace("--", "-"); // 将两个负号替换为一个  
                    textBox.Text = text;
            }
        }
    }
}
